package com.yinkgh.config;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
//import org.thymeleaf.spring4.SpringTemplateEngine;
//import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
//import org.thymeleaf.spring4.view.ThymeleafViewResolver;
//import org.thymeleaf.templateresolver.ITemplateResolver;


/**
 * springmvc配置同时支持html和jspl两种格式的页面
 */
//@Configuration
public class ViewResolverConfiguration {/*
	Logger logger = Logger.getLogger(ViewResolverConfiguration.class);
	
	@Configuration//用来定义 DispatcherServlet 应用上下文中的 bean
    @EnableWebMvc//EnableAutoConfiguration 会启用自动配置类 WebMvcAutoConfiguration，该类配置了一些默认的静态资源映射
    @ComponentScan("com.yinkgh")
    public class WebConfig extends WebMvcConfigurerAdapter {
        
		@Bean
        public ViewResolver viewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
//            resolver.setPrefix("/WEB-INF/");
//            resolver.setSuffix(".jsp");
//            resolver.setViewNames("jsps/*");
            System.out.println("配置viewResolver");
            resolver.setPrefix("/WEB-INF/jsp/");
            resolver.setSuffix(".jsp");
            resolver.setViewNames("*");
            resolver.setOrder(2);
            return resolver;
        }

        @Bean
        public ITemplateResolver templateResolver() {
        	System.out.println("配置templateResolver");
            SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
            templateResolver.setTemplateMode("HTML5");
            templateResolver.setPrefix("");
            templateResolver.setSuffix(".html");
            templateResolver.setCharacterEncoding("utf-8");
            templateResolver.setCacheable(false);
            logger.info("\n 进入templateResolver");
            return templateResolver;
        }

        @Bean
        public SpringTemplateEngine templateEngine() {
        	System.out.println("配置templateEngine");
            SpringTemplateEngine templateEngine = new SpringTemplateEngine();
            templateEngine.setTemplateResolver(templateResolver());
            // templateEngine
            logger.info("\n 进入templateEngine");
            return templateEngine;
        }

        @Bean
        public ThymeleafViewResolver viewResolverThymeLeaf() {
        	System.out.println("配置viewResolverThymeLeaf");
            ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
            viewResolver.setTemplateEngine(templateEngine());
            viewResolver.setCharacterEncoding("utf-8");
            viewResolver.setOrder(1);
            //viewResolver.setViewNames(new String[]{"thyme/*"});
            viewResolver.setViewNames(new String[]{"html/*","vue/*"});
            logger.info("\n 进入viewResolverThymeLeaf");
            return viewResolver;
        }

        @Override
        public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        	System.out.println("配置configureDefaultServletHandling");
            configurer.enable();
        }

        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
        	System.out.println("配置addResourceHandlers");
        	registry.addResourceHandler("/css/**").addResourceLocations("/css/");
        	registry.addResourceHandler("/fonts/**").addResourceLocations("/fonts/");
        	registry.addResourceHandler("/img/**").addResourceLocations("/img/");
        	registry.addResourceHandler("/js/**").addResourceLocations("/js/");
        	registry.addResourceHandler("/scss/**").addResourceLocations("/scss/");
            registry.addResourceHandler("/static/**").addResourceLocations("/WEB-INF/static/");
//            registry.addResourceHandler("").addResourceLocations("");
//            registry.addResourceHandler("").addResourceLocations("");
        	//配置静态资源访问路径 经过测试是生效的
        	registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
//            registry.addResourceHandler("/static/**").addResourceLocations("/WEB-INF/" + "/static/");
            //静态页面 配置如下内容就可以访问index.html页面
            registry.addResourceHandler("/index.html").addResourceLocations("/index.html");
        	//配置静态资源处理
//            registry.addResourceHandler("/**")
//                    .addResourceLocations("classpath:/resources2/", "classpath:/static2/", 
//                    "classpath:/public2/", "file:/webapps/");
            registry.addResourceHandler("/index.html")
            		.addResourceLocations("/index.html");
        	logger.info("\n 进入addResourceHandlers");
            super.addResourceHandlers(registry);
        }
    }

*/}
